package utils

import (
	"strings"
	"unicode"
)

// MySQL到Java数据类型的映射
var mysqlToJavaTypeMap = map[string]string{
	"VARCHAR":    "String",
	"CHAR":       "String",
	"TEXT":       "String",
	"TINYTEXT":   "String",
	"MEDIUMTEXT": "String",
	"LONGTEXT":   "String",
	"INT":        "Integer",
	"TINYINT":    "byte",
	"SMALLINT":   "Integer",
	"MEDIUMINT":  "Integer",
	"BIGINT":     "Long",
	"FLOAT":      "Float",
	"DOUBLE":     "Double",
	"DECIMAL":    "BigDecimal",
	"DATE":       "Date",
	"DATETIME":   "Date",
	"TIMESTAMP":  "Date",
	"TIME":       "Date",
	"YEAR":       "Integer",
	"BIT":        "Boolean",
	"BOOLEAN":    "Boolean",
	"BLOB":       "byte[]",
	"TINYBLOB":   "byte[]",
	"MEDIUMBLOB": "byte[]",
	"LONGBLOB":   "byte[]",
}

var mysqlToGoTypeMap = map[string]string{
	"INT":       "int",
	"TINYINT":   "int8",
	"SMALLINT":  "int16",
	"MEDIUMINT": "int32",
	"BIGINT":    "int64",
	"FLOAT":     "float32",
	"DOUBLE":    "float64",
	"DECIMAL":   "string",
	"CHAR":      "string",
	"VARCHAR":   "string",
	"TEXT":      "string",
	"DATE":      "time.Time",
	"DATETIME":  "time.Time",
	"TIMESTAMP": "time.Time",
	"TIME":      "time.Time",
	"YEAR":      "int",
	"ENUM":      "string",
	"SET":       "string",
	"BLOB":      "[]byte",
	"BIT":       "[]byte",
	"JSON":      "string",
}

var mysqlToTsTypeMap = map[string]string{
	"INT":       "number",
	"TINYINT":   "number",
	"SMALLINT":  "number",
	"MEDIUMINT": "number",
	"BIGINT":    "number",
	"FLOAT":     "number",
	"DOUBLE":    "number",
	"DECIMAL":   "string",
	"CHAR":      "string",
	"VARCHAR":   "string",
	"TEXT":      "string",
	"DATE":      "string",
	"DATETIME":  "string",
	"TIMESTAMP": "string",
	"TIME":      "string",
	"YEAR":      "number",
	"ENUM":      "string",
	"SET":       "string",
	"BLOB":      "Uint8Array",
	"BIT":       "Uint8Array",
	"JSON":      "any",
}

var mysqlToPythonTypeMap = map[string]string{
	"VARCHAR":   "str",
	"CHAR":      "str",
	"TEXT":      "str",
	"LONGTEXT":  "str",
	"INT":       "int",
	"TINYINT":   "int",
	"SMALLINT":  "int",
	"MEDIUMINT": "int",
	"BIGINT":    "int",
	"FLOAT":     "float",
	"DOUBLE":    "float",
	"DECIMAL":   "decimal.Decimal",
	"DATE":      "datetime.date",
	"TIME":      "datetime.time",
	"DATETIME":  "datetime.datetime",
	"TIMESTAMP": "datetime.datetime",
	"BLOB":      "bytes",
	"BOOLEAN":   "bool",
}

func StrToUpCamelCase(s string) string {
	camelCase := StrToCamelCase(s)
	return strings.ToUpper(camelCase[:1]) + camelCase[1:]
}

func StrToCamelCase(s string) string {
	var result string
	shouldCapitalize := false

	for _, r := range s {
		if r == '_' {
			shouldCapitalize = true
		} else {
			if shouldCapitalize {
				result += string(unicode.ToUpper(r))
				shouldCapitalize = false
			} else {
				result += string(r)
			}
		}
	}

	return result
}
func StrConvertMySqlToJava(mysqlType string) string {
	javaType, exists := mysqlToJavaTypeMap[strings.ToUpper(mysqlType)]
	if !exists {
		return "Unknown"
	}
	return javaType
}

func StrConvertMySqlToGo(mysqlType string) string {
	goType, exists := mysqlToGoTypeMap[strings.ToUpper(mysqlType)]
	if !exists {
		return "Unknown"
	}
	return goType
}

func StrConvertMySqlToTs(mysqlType string) string {
	tsType, exists := mysqlToTsTypeMap[strings.ToUpper(mysqlType)]
	if !exists {
		return "unknown"
	}
	return tsType
}

func StrConvertMySQLToPython(mysqlType string) string {
	mysqlType = strings.ToUpper(mysqlType)
	pythonType, exists := mysqlToPythonTypeMap[mysqlType]
	if !exists {
		return "Unknown"
	}
	return pythonType
}
